/**
 * jims
 */
package com.jims.master.modules.csm.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.ChineseCharToEnUtils;
import com.jims.master.modules.csm.dao.ExpClassDictDao;
import com.jims.master.modules.csm.dao.ExpNameDictDao;
import com.jims.master.modules.csm.entity.ExpClassDict;
import com.jims.master.modules.csm.entity.ExpNameDict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 消耗品名称字典Service
 *
 * @author 朱齐
 * @version 2017-03-01
 */
@Service
@Transactional(readOnly = true)
public class ExpNameDictService extends CrudService<ExpNameDictDao, ExpNameDict> {

    @Autowired
    private ExpClassDictDao expClassDictDao;

    public ExpNameDict get(String id) {
        return super.get(id);
    }

    public List<ExpNameDict> findList(ExpNameDict expNameDict) {
        return super.findList(expNameDict);
    }

    public Page<ExpNameDict> findPage(Page<ExpNameDict> page, ExpNameDict expNameDict) {
        return super.findPage(page, expNameDict);
    }

    /*
    * @ZhuQ
    * 保存查询expInfo中包含的expNameDictlist
    * */
    public Page<ExpNameDict> findPageByInfo(Page<ExpNameDict> page, ExpNameDict expNameDict) {
        expNameDict.updateOrgId();
        expNameDict.setPage(page);
        page.setList(dao.findListByInfo(expNameDict));
        return page;
    }

    /*
    * @zhuq
    * 保存exp_name_dict
    * */
    @Transactional(readOnly = false)
    public int saveExpName(ExpNameDict expNameDict) {
        //后台将消耗品类型的id转换成编码
        if (expNameDict.getExpType() != null && !expNameDict.getExpType().equals("")) {
            ExpClassDict expClassDict = expClassDictDao.get(expNameDict.getExpType());
            expNameDict.setExpCode(expClassDict.getClassCode());
        }

        String expType = expNameDict.getExpCode();
        //由消耗品类型(不足6位补X)+三位序数号+剂型+00组成消耗品code
        for (int i = expType.length(); i < 8; i++) {
            expType = expType + "X";
        }
        String codeNum = "";
        //获取消耗品序号
        String style = expNameDict.getExpStyle();
        expNameDict.setExpStyle(style+"00");
        int code = dao.findByCodeNo(expNameDict);
        if (code < 10) {
            codeNum = "00" + code;
        } else if (code >= 10 && code < 100) {
            codeNum = "0" + code;
        } else {
            codeNum = "" + code;
        }
        expNameDict.setExpCode(expType + codeNum + expNameDict.getExpStyle());
        expNameDict.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(expNameDict.getExpName()));
        expNameDict.setExpStyle(style);
        return super.save(expNameDict);
    }

    @Transactional(readOnly = false)
    public int save(ExpNameDict expNameDict) {
        return super.save(expNameDict);
    }

    @Transactional(readOnly = false)
    public int delete(ExpNameDict expNameDict) {
        return super.delete(expNameDict);
    }

    public ExpNameDict findByExpCode(String expCode) {
        return dao.findByExpCode(expCode);
    }
    /**
     * 加载消耗品列表
     * @param expNameDict
     * @return
     * @author
     */
    public List<ExpNameDict> getExpNameList(ExpNameDict expNameDict) {
        return dao.getExpNameList(expNameDict);
    }

    /**
     * 根据消耗品字典来过滤药品信息
     * @param page
     * @param expNameDict
     * @return
     */
    public Page<ExpNameDict> findPageByDict(Page<ExpNameDict> page, ExpNameDict expNameDict) {
        expNameDict.updateOrgId();
        expNameDict.setPage(page);
        page.setList(dao.findPageByDict(expNameDict));
        return page;
    }
}